package br.edu.ifrn.sgpapp.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.edu.ifrn.sgp.dtos.DocumentoDTO;
import br.edu.ifrn.sgp.dtos.ProcessoDTO;
import br.edu.ifrn.sgpapp.R;
import br.edu.ifrn.sgpapp.adapter.DocumentosListAdapter;
import br.edu.ifrn.sgpapp.rest.DocumentoREST;

public class ListarDocumentosActivity extends GenericActivity<List<DocumentoDTO>> {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_geral);
		processarSolicitacao();
	}
	
	@Override
	public List<DocumentoDTO> doInBackgroundTask(String... params) {
		ProcessoDTO processo = (ProcessoDTO) getIntent().getSerializableExtra(getString(R.string.extra_lista_documentos));
		
		if (processo != null){
			return (List<DocumentoDTO>) processo.getDocumentos();
		} else {
			return DocumentoREST.listarDocumentosUsuario(getUsuarioLogado().getId());
		}
	}

	@Override
	public void onPostExecuteTask(List<DocumentoDTO> documentos) {
		final ListView listaDocumentos = (ListView) findViewById(R.id.list);
		DocumentosListAdapter adapter = new DocumentosListAdapter(this, documentos);
		listaDocumentos.setAdapter(adapter);
		listaDocumentos.setEmptyView(findViewById(R.id.textEmptyView));
		
		listaDocumentos.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ListarDocumentosActivity.this, VerDocumentoActivity.class);
				intent.putExtra(getString(R.string.extra_documento), (DocumentoDTO) listaDocumentos.getAdapter().getItem(position));
				startActivity(intent);
			}
		});
	}
}
